package com.woniuxy.mapper;

import com.woniuxy.entity.Food;
import com.woniuxy.entity.foodinfo;
import com.woniuxy.vo.UserFoodOrderDetailVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FoodMapper {

    @Select("select * from food")
    public List<Food> findAll();

    @Insert("insert into foodinfo(status,userinfoId,foodId,userId) values(#{status},#{userinfoId},#{foodId},#{userId})")
    void addfoodOrder(foodinfo fi);

    //关联表查询
    @Select("SELECT f.*, fi.*, u.* FROM foodinfo fi " +
            "JOIN food f ON fi.foodId = f.foodId " +
            "JOIN user u ON fi.userId = u.user_id " +
            "WHERE fi.userId = #{userId}")
    @Results({
            // 映射 food 表字段
            @Result(property = "food.foodId", column = "foodId"),
            @Result(property = "food.foodName", column = "foodName"),
            @Result(property = "food.foodIntro", column = "foodIntro"),
            @Result(property = "food.price", column = "price"),
            @Result(property = "food.kcal", column = "kcal"),
            @Result(property = "food.foodImg", column = "foodImg"),

            // 映射 foodinfo 表字段
            @Result(property = "info.userinfoId", column = "userinfoId"),
            @Result(property = "info.foodId", column = "foodId"),
            @Result(property = "info.userId", column = "userId"),
            @Result(property = "info.createTime", column = "createTime"),
            @Result(property = "info.status", column = "status"),

            // 映射 user 表字段
            @Result(property = "user.userId", column = "user_id"),
            @Result(property = "user.name", column = "name"),
            @Result(property = "user.account", column = "account"),
            @Result(property = "user.sex", column = "sex"),
            @Result(property = "user.phone", column = "phone"),
            @Result(property = "user.email", column = "email")
    })
    List<UserFoodOrderDetailVo> findOrderDetailsByUserId(Integer userId);

    @Update("update foodinfo set status ='已完成'  where userinfoId = #{outTradeNo}")
    void updateByUserinfoId(String outTradeNo);

    @Update("update foodinfo set status ='已取消'  where userinfoId = #{outTradeNo}")
    void updateByUserinfoId2(String outTradeNo);
    @Select("select * from foodinfo where userinfoId = #{id}")
    foodinfo findfoodinfoByUserinfoId(String id);
}
